import 'package:fluent_ui/fluent_ui.dart';
import 'package:provider/provider.dart';

import '../view_type.dart';
import 'school_class_view_list.dart';
import 'school_class_view_model.dart';
import 'school_grade_subject_view_list.dart';
import 'school_grade_subject_view_model.dart';
import 'school_semester_view_list.dart';
import 'school_semester_view_model.dart';
import 'school_student_view_list.dart';
import 'school_student_view_model.dart';
import 'school_teacher_view_list.dart';
import 'school_teacher_view_model.dart';
import 'school_view_edit.dart';
import 'school_view_list.dart';
import 'school_view_model.dart';

class SchoolViewMain extends StatefulWidget {
  final ViewType type;
  final String? id;
  final int? tab;

  const SchoolViewMain({super.key, required this.type, this.id, this.tab});

  @override
  State<SchoolViewMain> createState() => _SchoolViewMainState();
}

class _SchoolViewMainState extends State<SchoolViewMain> {
  final tabs = <Tab>[];
  late int currentIndex;

  Tab createTab(int index, String title, Widget Function() handler) => Tab(
        text: Text(title),
        semanticLabel: title,
        body: handler.call(),
      );

  @override
  void initState() {
    super.initState();
    currentIndex = widget.tab ?? 0;
    final id = widget.id ?? '';
    //添加Tabs
    if (id.isNotEmpty) {
      tabs.addAll([
        createTab(0, '基本信息', () => SchoolViewEdit(id: id)),
        createTab(1, '学年学期', () => SchoolSemesterViewList(schoolId: id)),
        createTab(2, '年级科目', () => SchoolGradeSubjectViewList(schoolId: id)),
        createTab(3, '教师列表', () => SchoolTeacherViewList(schoolId: id)),
        createTab(4, '班级列表', () => SchoolClassViewList(schoolId: id)),
        createTab(5, '学生列表', () => SchoolStudentViewList(schoolId: id)),
      ]);
    }
  }

  @override
  Widget build(final BuildContext context) => MultiProvider(
        providers: [
          //基本信息
          ChangeNotifierProvider<SchoolViewModel>(
            create: (_) => SchoolViewModel(),
          ),
          //学校学期
          ChangeNotifierProvider<SchoolSemesterViewModel>(
            create: (_) => SchoolSemesterViewModel(),
          ),
          //学校年级科目
          ChangeNotifierProvider<SchoolGradeSubjectViewModel>(
            create: (_) => SchoolGradeSubjectViewModel(),
          ),
          //学校教师
          ChangeNotifierProvider<SchoolTeacherViewModel>(
            create: (_) => SchoolTeacherViewModel(),
          ),
          //学校班级
          ChangeNotifierProvider<SchoolClassViewModel>(
            create: (_) => SchoolClassViewModel(),
          ),
          //学校学生
          ChangeNotifierProvider<SchoolStudentViewModel>(
            create: (_) => SchoolStudentViewModel(),
          ),
        ],
        builder: (_, __) {
          //列表
          if (widget.type == ViewType.list) {
            return const SchoolViewList();
          }
          //新增
          if ((widget.id ?? '').isEmpty) {
            return const SchoolViewEdit();
          }
          //编辑
          return TabView(
            tabs: tabs,
            currentIndex: currentIndex,
            onChanged: (index) => setState(() => currentIndex = index),
            closeButtonVisibility: CloseButtonVisibilityMode.never,
            shortcutsEnabled: false,
          );
        },
      );
}
